package server;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author lbq
 * @version 1.0
 * @date 2021/1/22 20:30
 */
public class StaticResourceUtil {

    public static String getAbsolutePath(String path) {
        return StaticResourceUtil.class.getResource(path).getPath().substring(1);
    }

    public static void outputStaticSource(FileInputStream fileInputStream, OutputStream out) throws IOException {
        int count = 0;
        while (count == 0) {
            count = fileInputStream.available();
        }
        int sourceSize = count;
        out.write(HttpProtocolUtil.getHttpHeader200(sourceSize).getBytes());

        int len;
        byte[] bytes = new byte[1024];
        while ((len = fileInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, len);
        }
        fileInputStream.close();
        out.close();
    }
}
